import ApiManage from "./ApiManage";
import Config from "./Config";
import store from '@/store'
import {formatDateStr, routeTo} from "../utils/util";

class Api {
	constructor() {
		this.api = new ApiManage()
	}

	login({
			  userName,
			  password,
			  yzm
		  }) {
		let cid = "";
		// #ifdef APP-PLUS
		let pinf = plus.push.getClientInfo();
		cid = pinf.clientid; //客户端标识
		console.log(cid);
		// #endif
		return this.api.doPost(Config.LOGIN, {
			userName: userName,
			password: password,
			yzm: yzm,
			uuid: cid
		})
	}

	checkSid() {
		return this.api.doPost(Config.CHECK_SID, {})
	}

	apiQuery({
				 apiTag = 'D_',
				 apiNo,
				 page = 1,
				 row = 4,
				 id = '',
				 XM = ''
			 }) {

		return this.api.doPost(Config.QUERY_URL, {
			apiTag: apiTag,
			apiNo: apiNo,
			page: page,
			row: row,
			id: id,
			XM:XM
		})
	}

	apiQuery_public({
						apiTag = 'D_',
						apiNo,
						page = null,
						row = null,
						param
					}) {
		var newParam = Object.assign({
			apiTag: apiTag,
			apiNo: apiNo
		}, param);
		return this.api.doPost(Config.QUERY_URL, newParam)
	}


	getTaskList({
					page = 1,
					row = 10,
					wfid = "",
					query = ''
				}) {
		return this.api.doPost(Config.TASK_LIST, {
			page: page,
			row: row,
			wfid: wfid,
			query: query
		})
	}

	getHasDoIndex({
					  page = 1,
					  row = 10,
					  wfid = "",
					  query = ''
				  }) {
		return this.api.doPost(Config.HAS_DO_INDEX, {
			page: page,
			row: row,
			wfid: wfid,
			query: query
		})
	}

	getOpenTask({
					taskId,
					openStatus = 8
				}) {
		return this.api.doPost(Config.OPEN_TASK, {
			taskId: taskId,
			openStatus: openStatus
		})
	}

	getFileIds({
				   sourceDataId,
				   businessType = 'form',
				   fieldId,
			   }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_accessory_download_list.action',
			sourceDataId: sourceDataId,
			businessType: businessType,
			fieldId: fieldId,
		})
	}

	/**
	 *
	 * @param auditOption 选择的审核菜单id
	 * @param auditValue 意见留言
	 * @param taskId 任务id
	 * @param instanceId 实例id
	 * @param formDate 数据json字符串
	 * @param entityTableName 表名称
	 * @param openStatus
	 * @param formId 表单id
	 * @param mainId
	 * @returns {Promise<unknown>}
	 */
	saveFormData({
					 auditOption = '',
					 auditValue = '',
					 taskId,
					 instanceId,
					 formDate,
					 entityTableName,
					 openStatus = 8,
					 formId,
					 mainId
				 }) {
		return this.api.doPost(Config.SAVE_FORM_DATA, {
			...formDate,
			auditOption: auditOption,
			auditValue: auditValue,
			taskId: taskId,
			instanceId: instanceId,
			formDate: '',
			entityTableName: entityTableName,
			openStatus: openStatus,
			formId: formId,
			mainId: mainId
		})
	}

	saveAudit({
				  auditOption = '',
				  auditValue = '',
				  taskId
			  }) {
		return this.api.doPost(Config.SAVE_AUDIT, {
			auditOption: auditOption,
			auditValue: auditValue,
			taskId: taskId
		})
	}

	handleCheck_ck({
					   taskId,
					   stepId,
					   stepNo,
					   flowId
				   }) {
		return this.api.doPost(Config.HANDLE_CHECK_CK, {
			taskId: taskId,
			stepId: stepId,
			stepNo: stepNo,
			flowId: flowId,
		})
	}

	handleCheck({
					taskId,
					stepId,
					stepNo,
					flowId
				}) {
		return this.api.doPost(Config.HANDLE_CHECK, {
			taskId: taskId,
			stepId: stepId,
			stepNo: stepNo,
			flowId: flowId,
		})
	}

	taskSend({
				 flowId,
				 target,
				 title,
				 priority,
				 nextStepNo,
				 stepId,
				 taskId
			 }) {
		return this.api.doPost(Config.TASK_SEND, {
			flowId: flowId,
			target: target,
			title: title,
			priority: priority,
			nextStepNo: nextStepNo,
			stepId: stepId,
			taskId: taskId,
		})
	}

	getUserImg({
				   sourceDataId,
				   businessType = 'user',
				   fieldId = 'userImg',
				   sessionId
			   }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_accessory_download_list.action',
			sourceDataId: sourceDataId,
			businessType: businessType,
			fieldId: fieldId,
			sessionId: sessionId
		})
	}

	updateUserPwd({
					  oldpwd,
					  pwd,
					  pwd1,
					  sessionId
				  }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_system_updatepwd.action',
			oldpwd: oldpwd,
			pwd: pwd,
			pwd1: pwd1,
			sessionId: sessionId
		})
	}

	saveAddSignAfter({
						 signvalue,
						 taskId,
						 stepId,
						 flowId
					 }) {
		return this.api.doPost(Config.SAVE_ADDSIGN_AFTER, {
			taskId: taskId,
			stepId: stepId,
			flowId: flowId,
			signValue: signvalue,
		})
	}

	circulateAfter({
					   sessionId,
					   taskId,
					   stepId,
					   stepNo,
					   flowId
				   }) {
		return this.api.doPost(Config.CIRCULATEAFTER, {
			sessionId: sessionId,
			taskId: taskId,
			stepId: stepId,
			stepNo: stepNo,
			flowId: flowId
		})
	}

	getOrgTree({
				   isCom = true,
				   isDep = true
			   }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'htmlApi_advanced_getOrgTree.action',
			isCom: isCom,
			isDep: isDep
		})
	}

	getOrgTree_user(depId) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'htmlApi_advanced_getOrgTree_user.action',
			depId: depId
		})
	}

	getOrgTree_queryuser({
							 queryUser = ''
						 }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			queryUser: queryUser,
			p_url: 'htmlApi_advanced_getOrgTree_queryuser.action',
		})
	}

	addSignSend({
					sessionId,
					taskId,
					stepId,
					flowId,
					target,
					title
				}) {
		return this.api.doPost(Config.ADDSIGNSEND, {
			sessionId: sessionId,
			taskId: taskId,
			stepId: stepId,
			flowId: flowId,
			target: target,
			title: title,
			priority: 0
		})
	}

	modifiedTask({
					 sessionId,
					 taskId,
					 stepId,
					 stepNo,
					 flowId,
					 target,
					 title
				 }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			sessionId: sessionId,
			taskId: taskId,
			stepId: stepId,
			stepNo: stepNo,
			flowId: flowId,
			p_url: "sys_taskModified.action"
		})
	}

	getUserByIds({
					 userIds
				 }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'htmlApi_advanced_getOrgTree_queryuser_by_userids.action',
			userIds: userIds
		})
	}

	/**
	 * 保存自定义流程信息
	 */
	saveUserFlowDiy({
						flowIds
					}) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_api_update.action',
			apiTag: "D_",
			apiNo: "API2020000015",
			flowIds: flowIds,
		})
	}

	/**
	 * 发起一个待办流程
	 */
	addWfTask({
				  flowId
			  }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_wfTask_saveTask.action',
			flowId: flowId,
			title: "空标题"
		})
	}

	/**
	 * 计算修改天数
	 */
	setLeaveDay({
					leaveDay_field,
					startDate,
					startTime,
					endDate,
					endTime,
					fieldId
				}) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_component_leaveDay_getDays.action',
			startDate: startDate,
			startTime: startTime,
			endDate: endDate,
			endTime: endTime,
			fieldId: fieldId
		})
	}

	addSubData({
				   tablename,
				   instanceId
			   }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_wfRun_grid_add_def_json.action',
			tablename,
			instanceId
		})
	}

	deleteSubData({
					  ids,
					  tableName,
					  instanceId,
					  stepId,
					  flowId
				  }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_wfRun_delSubRecord.action',
			ids,
			tableName,
			instanceId,
			stepId,
			flowId
		})
	}

	uploadFile({
				   files,
				   filePath,
				   name = 'file',
				   formData
			   }) {
		return uni.uploadFile({
			url: Config.BASE_URL + Config.UPLOAD_FILE,
			name: name,
			filePath: filePath,
			formData: formData,
			files: files
		});
	}

	/**
	 *
	 * @param id 多条id空格分隔
	 * @returns {Promise | Promise<unknown>}
	 */
	deleteFile({
				   id
			   }) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_accessory_delete.action',
			id: id
		})
	}

	async checkUpdate({showTips = true}) {
		console.log(showTips)



		if (showTips) {
			uni.showLoading({
				title: '正在检查更新'
			})
		}
		let data = await this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: Config.CHECK_UPDATE
		});
		console.log(data)
		data = data.data.data;

		let versionInfo = store.getters['versionInfo'];

		console.debug('当前版本:' + versionInfo)
		console.debug('更新数据:', data)
		console.log(showTips)
		if (showTips) {
			uni.hideLoading({
				title: '正在检查更新'
			})
		}

		let isUpdate = UpdateUtil.compareVersion(versionInfo.version, data.APPVERSIONS)
		if (isUpdate) {

			if (showTips){
				let [error, res] = await uni.showModal({
					title: '检查更新',
					content: data.SJSM,
				})
				console.log(res)
				if (res.confirm) {
					uni.showLoading({
						mask: true,
						title: '开始下载更新'
					})

					uni.downloadFile({
						url: Config.BASE_URL + '/mobile/' + data.APPPACKAGE,
						success(res) {
							uni.hideLoading()
							plus.runtime.install(res.tempFilePath, {
								force: false
							}, function () {
								console.log('install success...');
								plus.runtime.restart();
							}, function (e) {
								console.error('install fail...', e);
							});

						}
					})
				}
			}else {
				function showModel(){
					plus.nativeUI.confirm(data.SJSM, function(e){
						if(e.index === 0){
							uni.showLoading({
								mask: true,
								title: '开始下载更新'
							})
							uni.downloadFile({
								url: Config.BASE_URL + '/mobile/' + data.APPPACKAGE,
								success(res) {
									uni.hideLoading()

									plus.runtime.install(res.tempFilePath, {
										force: false
									}, function () {
										console.log('install success...');
										plus.runtime.restart();
									}, function (e) {
										console.error('install fail...', e);
									});

								}
							})
						}else{
							showModel()
						}
					}, {
						"title":"检查更新",
						"buttons":['立即更新'],

					});
				}
				showModel()
			}


		} else {
			if (showTips) {
				uni.showToast({
					title: '已经是最新版本'
				})
			}
		}
	}

	homeNotice() {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: 'sys_api_query.action?apiTag=D_&apiNo=API2020000018'
		})
	}

	submitFeedback({YJ}) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: "sys_api_add.action?apiTag=D_&apiNo=API2020000019",
			YJ: YJ
		})
	}

	ckUpdatePwd() {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: "sys_user_getIsUpdatePwd.action"
		})
	}

	publicPost(url, params) {
		return this.api.doPost(Config.SYS_PUBLIC_POST, {
			p_url: url,
			...params
		})
	}

	getHysList() {
		return this.publicPost('sys_meeting_hys_list.action')
	}

	getMeetingList(params) {
		return this.publicPost('sys_meeting_getMeetingList.action', params)
	}

	getPcOperationList() {
		return this.publicPost(Config.PC_OPERATION_LIST)
	}

	getMobileOperationList() {
		return this.publicPost(Config.MOBILE_OPERATION_LIST)
	}

	circulate_send({flowId,stepId,taskId,target,title,priority}) {
	    return this.api.doPost(Config.SYS_PUBLIC_POST, {
	        p_url: "sys_wfTask_circulate_send.action"
			,flowId:flowId
			,stepId:stepId
			,taskId:taskId
			,target:target
			,title:title
			,priority
	    })
	}

	sys_meeting_sxbsj() {
		return this.publicPost('sys_meeting_sxbsj.action')
	}

	sys_meeting_addHysYd({HYMC, start, startTime, end, endTime, CYRS, HYMS, HYSID}) {
		let params = {
			HYMC: HYMC,
			STARTDATE: formatDateStr(start, 'YYYY-mm-dd'),
			STARTTIME: startTime,
			ENDDATE: formatDateStr(end, 'YYYY-mm-dd'),
			ENDTIME: endTime,
			CYRS: CYRS,
			HYMS: HYMS,
			HYSID: HYSID
		}
		return this.publicPost("sys_meeting_addHysYd.action", params)
	}

	getMySubscription({page, row = 10}) {
		return this.publicPost("sys_api_query.action?apiTag=D_&apiNo=API2020000023", {
			page: page,
			row: row
		})

	}

	sys_meeting_deleteMeeting({ID}) {
		return this.publicPost("sys_meeting_deleteMeeting.action", {
			id: ID
		})
	}

	getSignatureImg(){
		return this.publicPost("htmlApi_getMySignetBase64.action")
	}

	saveSigntureImg({imgData}){
		return this.publicPost("htmlApi_saveUploadSignet_ajax_base64.action",{
			base64:imgData
		})
	}

	removeSignature(){
		return this.publicPost("htmlApi_signet_deleteImg.action")
	}

	meetingSign({id}){
		return this.publicPost("sys_meeting_saveUserQd.action",{
			hyid:id
		})
	}

	meetingSignList({id}){
		return this.publicPost("sys_meeting_getQdListByHyId.action",{
			hyid:id
		})
	}
}

export default function install(Vue) {
	Vue.prototype.api = new Api()
}
